package com.kevin.ymqquan.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.kevin.ymqquan.MainActivity;
import com.kevin.ymqquan.R;
import com.kevin.ymqquan.application.App;
import com.kevin.ymqquan.constants.Constants;
import com.kevin.ymqquan.custom.CustomToast;
import com.kevin.ymqquan.custom.DialogUtil;
import com.kevin.ymqquan.httppost.Helper;
import com.kevin.ymqquan.httppost.HttpPostService;

public class LoginActivity extends Activity implements OnClickListener {

	private Button btn_login;

	private EditText et_login_username;
	private EditText et_login_password;

	private ImageView iv_login_username_logo;
	private ImageView iv_login_password_logo;

	private App application;

	private static final String LOGIN = "login_pref";

	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		init();
	}

	private void init() {

		application = (App) this.getApplication();

		btn_login = (Button) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(this);

		iv_login_username_logo = (ImageView) findViewById(R.id.iv_login_username_logo);
		iv_login_password_logo = (ImageView) findViewById(R.id.iv_login_password_logo);

		et_login_username = (EditText) findViewById(R.id.et_login_username);
		et_login_username.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if (arg1) {
					iv_login_username_logo
							.setBackgroundResource(R.drawable.login_num_green);
				} else {
					iv_login_username_logo
							.setBackgroundResource(R.drawable.login_num_gray);
				}

			}
		});

		et_login_password = (EditText) findViewById(R.id.et_login_password);
		et_login_password.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if (arg1) {
					iv_login_password_logo
							.setBackgroundResource(R.drawable.login_password_green);
				} else {
					iv_login_password_logo
							.setBackgroundResource(R.drawable.login_password_gray);
				}

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			if (Helper.checkConnection(LoginActivity.this)) {

				if ("000".equals(et_login_username.getText().toString())) {
					Intent i = new Intent(LoginActivity.this,
							MainActivity.class);
					startActivity(i);
					finish();
				}

				if ("".equals(et_login_username.getText().toString())) {
					CustomToast.CustomActivityToast(LoginActivity.this,
							"请输入用户名", -80);
				} else if ("".equals(et_login_password.getText().toString())) {
					CustomToast.CustomActivityToast(LoginActivity.this,
							"请输入密码", -80);
				} else if (!"".equals(et_login_username.getText().toString())
						&& !"".equals(et_login_password.getText().toString())) {
					if (Helper.checkConnection(LoginActivity.this)) {
						dialog = CustomToast.createLoadingDialog(
								LoginActivity.this, "登录中...");
						dialog.setCancelable(true);
						dialog.show();
						LoginAsynTask loginTask = new LoginAsynTask();
						loginTask.execute();

					} else {
						CustomToast.CustomActivityToast(LoginActivity.this,
								"请检查网络连接", -80);
					}

				}

			} else {
				DialogUtil.showMustNoticeDialog(LoginActivity.this, "请检查网络连接",
						false);
			}

			break;

		default:
			break;
		}
	}

	class LoginAsynTask extends AsyncTask<String, Object, String> {
		@Override
		protected String doInBackground(String... arg0) {
			String param = "{\"method\":\"userLogin\",\"userName\":\""
					+ et_login_username.getText().toString()
					+ "\",\"passWord\":\""
					+ et_login_password.getText().toString() + "\"}";
			String replyList = "";
			try {
				replyList = HttpPostService.PostJsonString(
						Constants.ServerAddr, param);
				// Log.i("kevinlog", "ok-----"+replyList);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return replyList;
		}

		@Override
		protected void onPostExecute(String JsonResult) {
			super.onPostExecute(JsonResult);
			if (JsonResult != null) {
				// String result = JsonResult.replace("\n", "");
				String result = JsonResult;
				JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(result);
					String resultType = jsonObject.getString("status");
					if ("0".equals(resultType)) {
						String userId = jsonObject.getString("userId");
						application.userId = Integer.parseInt(userId);
						Intent i = new Intent(LoginActivity.this,
								MainActivity.class);
						startActivity(i);
						finish();

						// String userId = jsonObject.getString("userId");
						// String userType = jsonObject.getString("userType");
						// String name = jsonObject.getString("name");
						// String sex = jsonObject.getString("sex");
						// String number = jsonObject.getString("number");
						// String studentClass = jsonObject.getString("class");
						// String academy = jsonObject.getString("academy");
						//
						//
						// application.userId = Integer.parseInt(userId);
						// application.userType = Integer.parseInt(userType);
						//
						SharedPreferences preferences = getSharedPreferences(
								LOGIN, Context.MODE_PRIVATE);
						Editor editor = preferences.edit();
						editor.putString("userId", userId);
						editor.putString("userName", et_login_username
								.getText().toString());
						editor.putString("passWord", et_login_password
								.getText().toString());
						editor.commit();
						//
						// Intent i = new
						// Intent(LoginActivity.this,MainActivity_bak.class);
						// startActivity(i);
						// finish();
					} else {
						CustomToast.CustomActivityToast(LoginActivity.this,
								"用户名或密码错误！", -80);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dialog.dismiss();
			}
		}
	}
}
